<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="25%"
      :close-on-click-modal="true"
      :close-on-press-escape="true"
      @close="resetForm"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm" size="mini">
        <el-form-item label="采集器编号" prop="collect_num">
          <el-input v-model="ruleForm.collect_num" clearable placeholder="请输入采集器编号"/>
        </el-form-item>
        <el-form-item label="采集器名称" prop="collect_name">
          <el-input v-model="ruleForm.collect_name" clearable placeholder="请输入采集器名称"/>
        </el-form-item>
        <el-form-item label="安装地址" prop="address">
          <el-input v-model="ruleForm.address" clearable placeholder="请输入安装地址"/>
        </el-form-item>
        <el-form-item label="采集器分类" prop="category">
          <el-select v-model="ruleForm.category" placeholder="请选择" size="mini">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.cate_name"
              :value="item.id"
            />
          </el-select>
          <!-- <el-input v-model="ruleForm.description" clearable placeholder="请输入类型描述" /> -->
        </el-form-item>
        <el-form-item label="状态" prop="collect_status">
          <el-select v-model="ruleForm.collect_status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属企业" prop="company_id">
          <el-select v-model="ruleForm.company_id" placeholder="请选择">
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.company_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="display: flex;justify-content: flex-end">
          <el-button type="primary" @click="resetForm">取消</el-button>
          <el-button type="primary" @click="addOrEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  dataCategoryList,
  dataDictionariesList,
  companyListData,
  dataCreate, dataDetail,
  dataUpdate } from '@/api/collector'
export default {
  name: 'detail',
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      ruleForm: {
        collect_num: '',
        collect_name: '',
        category: '',
        collect_status: '',
        company_id: '',
        address: ''
      },
      options: [],
      categoryList: [],
      companyList: [],
      rules: {
        collect_num: [
          { required: true, message: '请输入采集器编号', trigger: 'blur' }
        ],
        collect_name: [
          { required: true, message: '请输入采集器名称', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入安装地址', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '请选择采集器类型', trigger: 'blur' }
        ],
        company_id: [
          { required: true, message: '请选择所属企业', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择采集器分类', trigger: 'blur' }
        ],
        collect_status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.CategoryList()
    this.dictionariesList()
    this.dataCompanyList()
    // 编辑
    if (this.title === '新增') {
      this.ruleForm.pid = this.id
    } else {
      this.getDetailById(this.id)
    }
  },
  methods: {
    // 获取详情
    getDetailById(id) {
      dataDetail({ id }).then(res => {
        this.ruleForm.collect_num = res.data.collect_num
        this.ruleForm.collect_name = res.data.collect_name
        this.ruleForm.category = parseInt(res.data.category)
        this.ruleForm.collect_status = res.data.collect_status + ''
        this.ruleForm.id = res.data.id
        this.ruleForm.address = res.data.address
        this.ruleForm.company_id = res.data.company_id
      })
    },

    // 数据字典列表
    dictionariesList() {
      dataDictionariesList({ pid: 5, limit: 20, offset: 1 }).then(res => {
        console.log(' res.data.data.dictionaries res.data.data.dictionaries', res.data)
        if (res.code == 200) {
          this.options = res.data.dictionaries
        }
      })
    },
    // 分类列表
    CategoryList() {
      dataCategoryList({ pid: 1, limit: 20, offset: 1, order: 'ascending' }).then(res => {
        if (res.code == 200) {
          this.categoryList = res.data.categories
        }
      })
    },
    // 企业列表
    dataCompanyList() {
      companyListData({ pid: 1, limit: 20, offset: 1, order: 'ascending' }).then(res => {
        if (res.code == 200) {
          this.companyList = res.data.companies
        }
      })
    },
    // 新增、编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.title === '编辑') {
            //  是编辑
            dataUpdate(this.ruleForm).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          } else {
            //  是新增
            dataCreate(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          }
        }
      })
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('close')
    },
    handleChange(e) {
      console.log(e)
    }
  }
}
</script>

<style lang="scss" scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 10px;
    border: 1px dashed #cccccc;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 10px;
  }
</style>
